package com.moqianfan.mcuassistant.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.moqianfan.mcuassistant.R;
import com.moqianfan.mcuassistant.bean.User;
import com.moqianfan.mcuassistant.net.GetDataService;
import com.moqianfan.mcuassistant.utils.LogUtil;

import net.youmi.android.nm.sp.SplashViewSettings;
import net.youmi.android.nm.sp.SpotListener;
import net.youmi.android.nm.sp.SpotManager;
import net.youmi.android.nm.sp.SpotRequestListener;
import net.youmi.android.os.PointsManager;

import java.util.Calendar;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by limh on 2017/4/5.
 */

public class LogoShowActivity extends BaseActivity {

    private String TAG = "LogoShowActivity";
    private RelativeLayout realive;
    private SplashViewSettings splashViewSettings;
    private TextView txtJump;

    @Override
    public void findViews() {
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_logo_show);
        realive = (RelativeLayout) findViewById(R.id.realive);
        txtJump = (TextView) findViewById(R.id.txt_jump);
    }

    @Override
    public void initViews() {
        splashViewSettings = new SplashViewSettings();
        splashViewSettings.setAutoJumpToTargetWhenShowFailed(true);
        splashViewSettings.setTargetClass(MainActivity.class);
        splashViewSettings.setSplashViewContainer(realive);
        SpotManager.getInstance(this).requestSpot(new SpotRequestListener() {
            @Override
            public void onRequestSuccess() {
                LogUtil.d(TAG, "onRequestSuccess");
                handler.sendEmptyMessage(0x23);
            }

            @Override
            public void onRequestFailed(int i) {
                LogUtil.d(TAG, "onRequestFailed");
                handler.sendEmptyMessage(0x24);
            }
        });

        txtJump.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                handler.sendEmptyMessage(0x24);
            }
        });

        realive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        Calendar calendar = Calendar.getInstance();
        String day = "" + calendar.get(Calendar.DATE);
        LogUtil.d(TAG, "day=" + day);
        if (userPreference.getInt(day, -1) != -1) {
            userPreference.edit().putInt(day, 10).apply();
        }
        LogUtil.d(TAG, "ONCE=" + userPreference.getInt(day, -1));
    }

    @Override
    public void initDatas() {
        Calendar c = Calendar.getInstance();
        LogUtil.w(TAG, "signTime=" + userPreference.getInt("signTime", -1));
        LogUtil.w(TAG, "date.getDay()=" + c.get(Calendar.DATE));
        final int day = c.get(Calendar.DATE);
        if (userPreference.getInt("signTime", -1) != day) {
            //每天登陆获取1U币
            if (user != null) {
                PointsManager.getInstance(this).awardPoints(1);
                LogUtil.d(TAG, "本地积分：" + user.getScore());
                User newUser = new User();
                newUser.setScore(Math.max((int) PointsManager.getInstance(this).queryPoints(), user.getScore()));
                newUser.update(user.getObjectId(), new UpdateListener() {
                    @Override
                    public void done(BmobException e) {
                        if (null == e) {
                            LogUtil.e(TAG, "签到成功");
                            userPreference.edit().putInt("signTime", day).apply();
                            user = BmobUser.getCurrentUser(User.class);
                        } else {
                            LogUtil.e(TAG, "签到失败：" + e.toString());
                        }
                    }
                });
            } else {
                if (userPreference.getBoolean("isAdd", true)) {
                    if (PointsManager.getInstance(this).queryPoints() == 0)
                        PointsManager.getInstance(this).awardPoints(10);
                    userPreference.edit().putBoolean("isAdd", false).apply();
                }
            }

        } else {
            LogUtil.e(TAG, "今天已签到");
        }
    }

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            openService(GetDataService.class);
            switch (msg.what) {
                case 0x23:
                    SpotManager.getInstance(LogoShowActivity.this).showSplash(LogoShowActivity.this, splashViewSettings, new SpotListener() {
                        @Override
                        public void onShowSuccess() {
                            LogUtil.d(TAG, "展示成功");
                        }

                        @Override
                        public void onShowFailed(int i) {
                            LogUtil.d(TAG, "展示失败：" + i);
                        }

                        @Override
                        public void onSpotClosed() {
                            LogUtil.d(TAG, "onSpotClosed：");
                        }

                        @Override
                        public void onSpotClicked(boolean b) {
                            LogUtil.d(TAG, "onSpotClicked：");
                            PointsManager.getInstance(LogoShowActivity.this).awardPoints(10);
                        }
                    });
                    break;
                case 0x24:
                    openActivity(MainActivity.class);
                    finish();
                    break;
            }
            return true;
        }
    });

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SpotManager.getInstance(this).onDestroy();
    }
}
